import cookie from '@/utils/cookie';

export default class TioCookies {
  constructor() {
    this.init();
  }

  cks: Record<string, any> = {};

  REGEX = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;

  isIp(str: string): boolean {
    return this.REGEX.test(str);
  }

  init() {
    const initCookies = cookie.getAllCookies();
    if (initCookies) {
      this.cks = initCookies;
    }
  }

  getCookie(key: string) {
    return this.cks[key] || cookie.getCookie(key);
  }

  getAllCookies() {
    return this.cks || cookie.getAllCookies();
  }

  setCookie(key: string, value: any, options?: any) {
    this.cks[key] = value;
    cookie.setCookie(key, value, options);
  }

  removeCookie(key: string) {
    delete this.cks[key];
    cookie.removeCookie(key);
  }
}
